import { APP_ID, BASE_URL, XSRF_HEADER, REQUEST_TIMEOUT, PREFIX, SUCCESS_CODE } from './api/config'
import { getXXData } from './api/common.model'

export default {
  // Target: https://go.nuxtjs.dev/config-target
  target: 'static',

  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: '{{title}}',
    htmlAttrs: {
      lang: 'en',
      amp: true
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { name: 'format-detection', content: 'telephone=no,email=no' },
      { name: 'robots', content: 'index,follow' },
      { name: 'baidu-site-verification', content: 'hAHWaQJlwt' }
    ],
    link: [
      { rel: 'preconnect', href: '//unpkg.com', crossorigin: true },
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
    ],
    script: [
      {src: '//unpkg.com/jquery@3.5.1/dist/jquery.js'},
      {src: '//unpkg.com/vue@2.6.14/dist/vue.js'},
    ]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [
    // 'static/css/animate.min.css',
    // 'static/css/main.css',
    'assets/style/index.less'
  ],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: ['@/plugins/ui'],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  hooks: {
    'generate:page': result => {
      // delete data-n html string
      result.html = result.html.replace(/ data-n-head-ssr/, '').replace(/ data-n-head="\S*"/gi, '').replace(/ data-hid="\S*"/gi, '')
    }
  },

  // loading: '@/components/loading.vue',

  loadingIndicator: '@/skeleton.html', // html文件 骨架适合h5

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/eslint
    '@nuxtjs/eslint-module',
    // https://go.nuxtjs.dev/stylelint
    '@nuxtjs/stylelint-module',
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/axios
    '@nuxtjs/axios',
    // https://go.nuxtjs.dev/content
    '@nuxt/content',
    '@nuxtjs/device',
    '@nuxtjs/sitemap'
  ],

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    // Workaround to avoid enforcing hard-coded localhost:3000: https://github.com/nuxt-community/axios-module/issues/308
    baseURL: '/',
  },

  // Content module configuration: https://go.nuxtjs.dev/config-content
  content: {},

  // Sitemap SEO module configuration
  // sitemap: {
  //   hostname: 'https://www.xxx.com',
  //   cacheTime: 24 * 60 * 60 * 1000,
  //   defaults: {
  //     changefreq: 'daily',
  //     priority: 1,
  //     lastmod: new Date()
  //   },
  // },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    devtools: true,
    extractCSS: true,
    transpile: [],
    optimization: {
      runtimeChunk: true, // 'multiple',
      splitChunks: {
        name: true,
        cacheGroups: {
          'vue': {
            test: /vue/,
            enforce: true,
            name: 'vue-cdn',
            chunks: 'all'
          },
          'wattnetwork': {
            test: /@wattjs\/network/,
            enforce: true,
            name: 'wattnetwork-cdn',
            chunks: 'all'
          },
          'nuxt': {
            test: /nuxt/,
            enforce: true,
            name: 'nuxt-cdn',
            chunks: 'all'
          }
        }
      }
    },
    extend(config, { isDev }) {
      if (isDev) {
        config.devtool = 'source-map'
      }
    }
  },

  generate: {
    // routes(callback) {
    //   // 根据接口获得
    //   axios.get('').then(res => {
    //     const routes = res.data.map(r => `/news/${r.id}`)
    //     callback(null, routes)
    //   }).catch(callback)
    // },
    routes() {
      return ['/news/active/1', '/news/active/2']
    }
  },

  // 公共配置
  publicRuntimeConfig: {
    BASE_URL, // 请求域名
    PREFIX, // 请求公共前缀
    REQUEST_TIMEOUT, // 超时
    SUCCESS_CODE, // 正常code
    XSRF_HEADER, // cookie key
    APP_ID // 唯一id
  },

  router: {
    middleware: 'context',
  },
}
